package bizlogic;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import model.LeaveDTO;
import model.PublicHolidayDTO;
import dao.DAOException;
import dao.DAOfactory;
import dao.LeaveDAO;
import dao.PublicHolidayDAO;
import dao.mysql.DAOFactoryImpl;

public class LeaveManager {

	private LeaveDAO leaveDAO;
	
	public LeaveManager() {
		leaveDAO = DAOfactory.loadInstance().getLeaveDAO();
	}
	
	
	@SuppressWarnings("finally")
	public LeaveDTO findLeave(int empid){
		LeaveDTO currentLeave = new LeaveDTO();
		try {
			currentLeave = leaveDAO.findLeave(empid);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			return currentLeave;
		}
		
	}
	public ArrayList<LeaveDTO> findLeaveforEmployee(int employeeID){
		ArrayList<LeaveDTO> currentLeave = new ArrayList<LeaveDTO>();
		try {
			currentLeave = leaveDAO.findLeaveforEmployee(employeeID);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			return currentLeave;
		}
	
	}
	
	public LeaveDTO DeleteLeave(LeaveDTO leave){
		LeaveDTO currentLeave = new LeaveDTO();
		try {
			currentLeave = leaveDAO.DeleteLeave(leave);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			return currentLeave;
		}
		
	}

    @SuppressWarnings("finally")
	public ArrayList<LeaveDTO> findAllLeave() {
    	ArrayList<LeaveDTO> currentList = new ArrayList<LeaveDTO>();
		try {
			currentList = leaveDAO.findAllLeave();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			return currentList;
		}
    }
  /*  public ArrayList<LeaveDTO> FindLeaveHistory(int empid)
    {
    	ArrayList<LeaveDTO> historylist= new ArrayList<LeaveDTO>();
    	historylist= leaveDAO.findleavehistory(empid);
    			
    	return historylist;
    	
    }
    */
    public void insertLeave(LeaveDTO leave) throws SQLException {
    	try {
			leaveDAO.insertLeave(leave);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public void updateLeave(LeaveDTO leave) {
    	try {
			leaveDAO.updateLeave(leave);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public void deleteLeave(LeaveDTO leave) {
    	try {
			leaveDAO.deleteLeave(leave);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    
    
    
    
    @SuppressWarnings("deprecation")
	public int calculate(java.util.Date startdate, java.util.Date enddate) {
    	
    	//System.out.println("Start date in milis============ " + startdate.toString() +"      "+ startdate.getYear()+"    "+startdate.getMonth()+ "   "+startdate.getDay() );
    	//System.out.println("Start date in milis============ " + enddate.toString() +"      "+ enddate.getYear()+"    "+enddate.getMonth()+ "   "+enddate.getDay());
    	
    	
		int day = 0;
		
		StringTokenizer stk = new StringTokenizer(startdate.toString(), "-");
		int i =0;
		String[] startdatelist = new String[3];
		while (stk.hasMoreTokens())
		{
			startdatelist[i] = stk.nextToken();
			i++;
		}
		
		int startyear = Integer.parseInt(startdatelist[0]);
		int startmonth = Integer.parseInt(startdatelist[1]);
		int startday = Integer.parseInt(startdatelist[2]);				
		
		Calendar st = new GregorianCalendar();
		st.set(startyear,startmonth,startday);
		long d1 = st.getTimeInMillis();
		
		StringTokenizer stk2 = new StringTokenizer(enddate.toString(), "-");
		int j =0;
		String[] enddatelist = new String[3];
		while (stk2.hasMoreTokens())
		{
			enddatelist[j] = stk2.nextToken();
			j++;
		}
		
		int endyear = Integer.parseInt(enddatelist[0]);
		int endmonth = Integer.parseInt(enddatelist[1]);
		int endday = Integer.parseInt(enddatelist[2]);				

		Calendar en = new GregorianCalendar();
		en.set(endyear,endmonth,endday);
		long d2 = en.getTimeInMillis();

		day = (int) ((d2 - d1) / (24 * 60 * 60 * 1000));

		
		return day;

	}

	public int calculate2(java.util.Date startdate, java.util.Date enddate) {
		int day = 0;
		StringTokenizer stk = new StringTokenizer(startdate.toString(), "-");
		int i =0;
		String[] startdatelist = new String[3];
		while (stk.hasMoreTokens())
		{
			startdatelist[i] = stk.nextToken();
			i++;
		}
		
		int startyear = Integer.parseInt(startdatelist[0]);
		int startmonth = Integer.parseInt(startdatelist[1]);
		int startday = Integer.parseInt(startdatelist[2]);				
		
		Calendar st = new GregorianCalendar();
		st.set(startyear,startmonth,startday);
		long d1 = st.getTimeInMillis();
		
		StringTokenizer stk2 = new StringTokenizer(enddate.toString(), "-");
		int j =0;
		String[] enddatelist = new String[3];
		while (stk2.hasMoreTokens())
		{
			enddatelist[j] = stk2.nextToken();
			j++;
		}
		
		int endyear = Integer.parseInt(enddatelist[0]);
		int endmonth = Integer.parseInt(enddatelist[1]);
		int endday = Integer.parseInt(enddatelist[2]);				

		Calendar en = new GregorianCalendar();
		en.set(endyear,endmonth,endday);
		long d2 = en.getTimeInMillis();

		day = (int) ((d2 - d1) / (24 * 60 * 60 * 1000));
		

		DAOfactory f = new DAOFactoryImpl();
		PublicHolidayDAO pbDAO = f.getPublicHolidayDAO();
		ArrayList<PublicHolidayDTO> pbList = new ArrayList<PublicHolidayDTO>();
		try {
			pbList = pbDAO.findAllPublicHoliday();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (st.before(en)) {
			if (st.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
					|| st.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
				day--;
			}

			for (PublicHolidayDTO pbhol : pbList) {

				if (st.equals(pbhol.getDate())) {
					day--;
				}

				st.add(Calendar.DAY_OF_MONTH, 1);
			}
		}
		return day;
	}

}
